// miniprogram/pages/chat/chat.js
const app = getApp()
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Goods:[],
    User:[],
    send:true,
    title:'',

    Openids:[],
    Times:[],
    Titles:[],
    myOpenid:'',

    id:'',
    SellAvatarUrl:'',
    SellNickName:'',
    SellOpenid:'',
    avatarUrl:'',
    nickName:'',
    openid:'',

    // // 验证是否创建
    // Create:false,

  },
 
  return:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  input:function(e){
    console.log(e.detail.value)
    if (e.detail.value != "") {
      this.setData({
        send: false
      })
    } else {
      this.setData({
        send: true
      })
    }
    this.setData({
      title:e.detail.value
    })
  },
  SendMessage:function(){
    var that=this
    that.setData({
      Openids:that.data.Openids.concat(that.data.myOpenid),
      Times: that.data.Times.concat(that.getNowDatTime()),
      Titles: that.data.Titles.concat(that.data.title),
    })
    console.log(that.data.Openids)
    console.log(that.data.Times)
    console.log(that.data.Titles)
    wx.cloud.callFunction({
      name: "News",
      data: {
        id:that.data.id,
        Openids: that.data.Openids,
        Times:that.data.Times,
        Titles:that.data.Titles,
      }
    }).then(res => {
      console.log(res.data);
      that.setData({
        title:''
      })
    }).catch(err => {
      console.log(err);
    })
    
  },
  CreateChat:function(ShoopId){
    var that=this
    db.collection('Chat').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        nickName: app.user.nickName,
        avatarUrl: app.user.avatarUrl,
        SellOpenid: that.data.Goods[0].openid,
        SellNickName: that.data.Goods[0].nickName,
        SellAvatarUrl: that.data.Goods[0].avatarUrl,
        GoodsId:that.data.Goods[0].id,
        Times:[],
        Openids:[],
        Titles:[]
      }
    })
    .then(res => {
      console.log(res)
      that.getNews(ShoopId,that.data.myOpenid)
    })
    
  },
 
 //获取当前时间
 getNowDatTime:function(){
  var date = new Date();
  var seperator1 = "-";
  var seperator2 = ":";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate<=9) {
    strDate = "0" + strDate;
  }
  var time = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
  console.log(time);
  return time;
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    console.log(options.id)
    console.log(app.user.openid)
    console.log(app.user.nickName)
    console.log(app.user.avatarUrl)
    that.getGoods(options.id)
    // this.getNews(options.id)
    that.setData({
      'User[0].openid':app.user.openid,
      'User[0].nickName':app.user.nickName,
      'User[0].avatarUrl':app.user.avatarUrl,
      myOpenid:app.user.openid
    })
    console.log(this.data.User)
  },
  // 获取商品详细信息
 getGoods: function (ShoopId) {
  var that=this
  console.log(ShoopId)
  db.collection('Goods').doc(ShoopId)
    .get().then(res => {
      console.log(res.data.fileIDs)
      this.setData({
        'Goods[0].img': res.data.fileIDs[0],
        'Goods[0].id': res.data._id,
        'Goods[0].title': res.data.title,
        'Goods[0].rmb': res.data.rmb,
        'Goods[0].nickName':res.data.nickName,
        'Goods[0].avatarUrl':res.data.avatarUrl,
        'Goods[0].openid':res.data._openid,
      })
      db.collection('Chat').where({
        GoodsId: ShoopId,
        _openid:that.data.myOpenid,
      })
      .get({
        success: function(res) {
          console.log(res.data)
          if(res.data.length==0){
            that.CreateChat(ShoopId)
          }else{
            that.getNews(ShoopId,that.data.myOpenid)
          }
          
        }
      })
      console.log(this.data.Goods)
    })
    .catch(err => {
      console.error(err)
    
      wx.hideLoading();
    })
  },
  getNews:function(ShoopId,openid){
    var that = this
    console.log(app.user.openid)
    console.log(openid)
    db.collection('Chat').where({
      GoodsId: ShoopId,
      _openid: openid,

    })
    .get({
      success: function(res) {
        console.log(res.data)
        that.setData({
          id:res.data[0]._id,
          Openids:res.data[0].Openids,
          Times:res.data[0].Times,
          Titles:res.data[0].Titles,
          SellAvatarUrl:res.data[0].SellAvatarUrl,
          SellNickName:res.data[0].SellNickName,
          SellOpenid:res.data[0].SellOpenid,
          avatarUrl:res.data[0].avatarUrl,
          nickName:res.data[0].nickName,
          openid:res.data[0]._openid
        })
        console.log(res.data.Openids)
        console.log(res.data.Times)
        console.log(res.data.Titles)
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})